"""
Case Type   : rowcount_big()函数
Case Name   : pdbc驱动使用rowcount_big函数
Create At   : 2025/08/01
Owner       : zym_2025
Description :
    1、解压PDBC得到lib,替换psycopg2;
    2、配置lib;
    3、输入python3,输入import psycopg2
    4、新建用户并赋权,新建数据库;
    5、连接D库,执行sql语句;
    6、清理环境;
Expect      :
    1、执行成功;
    2、执行成功;
    3、执行成功;
    4、执行成功;
    5、执行成功;
    6、清理环境成功;
History     :
"""

import os
import unittest
import subprocess

from yat.test import macro

from testcase.CONNECTORS.PDBC.PSYCOPG2. \
    Opengauss_Function_Connect_Psycopg_Common import Psycopg2Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

class RowcountBig(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info('--OpenGauss_Compatibility_Function_Rowcount_Big_Case0033 start--')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_d'
        self.user = 'u_psycopg1'
        self.u_pwd = macro.COMMON_PASSWD
        self.conn_info = {'dbname': self.db_name,
                          'user': self.user,
                          'password': self.u_pwd,
                          'host': self.comsh.node.ssh_host,
                          'port': self.comsh.node.db_port}
        self.psycopg_com = Psycopg2Common(**self.conn_info)
        self.table_name = 't_rowcount_0033'

    def testRowcountBig(self):
        text = '------step1:解压PDBC得到lib,替换psycopg2;expect: 执行成功------'
        self.log.info(text)
        res = self.psycopg_com.decompress_pkg()
        self.assertTrue(res, f'执行失败: {text}')
        res = self.psycopg_com.set_new_psycopg2()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step2: 配置lib expect: 成功----'
        self.log.info(text)
        res = self.psycopg_com.set_lib_path()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step3: 输入python3,输入import psycopg2 expect: 执行成功----'
        self.log.info(text)
        cmd = f'python3 -c "import psycopg2"'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.assertNotIn("No module named 'psycopg2'", res, f'执行失败: {text}')

        text = '------step4:新建用户并赋权,新建数据库;expect:执行成功------'
        self.log.info(text)
        sql = f'''create user {self.user} with login password '{self.u_pwd}'; grant all privileges to {self.user};
                  drop database if exists {self.db_name};create database {self.db_name} dbcompatibility 'D';'''
        res = self.comsh.execut_db_sql(sql)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res, '新建用户失败')
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, res, '用户赋权失败')
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, 'D库创建失败')
        sql = "create extension shark;"
        res = self.comsh.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn(self.constant.create_extension_success, res, '创建shark插件失败')

        text = '------step5:连接D库,执行sql语句;expect:成功------'
        self.log.info(text)
        res = self.psycopg_com.conn_db()
        expect = f"user={self.user} " \
            f"dbname={self.db_name} " \
            f"host={self.comsh.node.ssh_host} " \
            f"port={self.comsh.node.db_port}"
        res = [self.assertIn(i, res) for i in expect.split()]
        self.assertEqual(len(res), 4, f'连接数据库失败')
        sql = f"DROP TABLE IF EXISTS {self.table_name};" \
              f"CREATE TABLE {self.table_name} (ID INT PRIMARY KEY,Value NVARCHAR(50));" \
              f"INSERT INTO {self.table_name} VALUES (1, 'A'), (2, 'B'), (3, 'C');" \
              f"select rowcount_big();"
        f_sql = self.psycopg_com.format_sql(sql)
        run_sql = self.psycopg_com.join_run_sql(f_sql)
        res = self.psycopg_com.run_sql(sql=run_sql)
        self.log.info(res)
        self.assertIn('[(3,)]', res, 'rowcount_big()查询异常')

        sql = f"select * from {self.table_name};" \
              f"select rowcount_big();"
        f_sql = self.psycopg_com.format_sql(sql)
        run_sql = self.psycopg_com.join_run_sql(f_sql)
        res = self.psycopg_com.run_sql(sql=run_sql)
        self.log.info(res)
        self.assertIn('[(3,)]', res, 'rowcount_big()查询异常')

    def tearDown(self):
        text = '------step6:清理环境;expect:成功------'
        self.log.info(text)
        self.log.info('------删除解压文件目录;expect:成功------')
        res = self.psycopg_com.clean_decompress_file()
        self.assertTrue(res, f'删除解压文件目录失败')

        self.log.info('-------删除表,D库,用户;expect:成功------')
        sql = f'drop table {self.table_name};'
        res = self.comsh.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')
        sql = f'drop database {self.db_name}; drop user {self.user} cascade;'
        res = self.comsh.execut_db_sql(sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删库失败')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, res, '删除用户失败')
        self.log.info(f'------OpenGauss_Compatibility_Function_Rowcount_Big_Case0033 end------')